package cn.dnui_xqh414.dnuifood_16110100414;

import androidx.appcompat.widget.Toolbar;
import androidx.core.widget.NestedScrollView;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import cn.dnui_xqh414.dnuifood_16110100414.Fragment.ShopCommentsFragment;
import cn.dnui_xqh414.dnuifood_16110100414.Fragment.ShopFoodListFragment;
import cn.dnui_xqh414.dnuifood_16110100414.bean.CollectBean;
import cn.dnui_xqh414.dnuifood_16110100414.bean.Collectstate;
import cn.dnui_xqh414.dnuifood_16110100414.fragment_adapter_and_vhoder.MyFragmentAdapter;
import cn.dnui_xqh414.dnuifood_16110100414.listener.OnRetrofitListener;
import cn.dnui_xqh414.dnuifood_16110100414.model.ShopModel;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.tabs.TabLayout;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

public class ShopFoodListActivity
        extends BaseActivity
        implements TabLayout.OnTabSelectedListener{

    private TextView tv_shopid;
    private NestedScrollView scrollView;

    private MenuItem menuItem;

    private ViewPager vp_shopfoodList;
    private TabLayout tabLayout;

    private ImageView imageView;
    private Toolbar toolbar;

    private MyFragmentAdapter adapter;
    private String[] titles = new String[]{"foodlist","detail"};
    private List<Fragment> fragmentList = new ArrayList<>();

    private ShopModel shopModel = new ShopModel();
    private String favFlag = "-1";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    void initViews() {
        setContentView(R.layout.activity_shopfoodlist);
        vp_shopfoodList = findViewById(R.id.vp_shop_food_list);
        tabLayout = findViewById(R.id.tblt_shop_food_list);
        imageView = findViewById(R.id.shopphoto);
        toolbar = findViewById(R.id.foodlist_toolbar);
        setSupportActionBar(toolbar);
        try{
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }catch (NullPointerException e){
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
        }


        scrollView = findViewById(androidx.appcompat.R.id.scrollView);

        for (String title:titles)
            tabLayout.addTab(tabLayout.newTab().setText(title));
        tabLayout.addOnTabSelectedListener(this);

        fragmentList.add(new ShopFoodListFragment());
        fragmentList.add(new ShopCommentsFragment());

        adapter = new MyFragmentAdapter(getSupportFragmentManager(),titles,fragmentList);
        vp_shopfoodList.setAdapter(adapter);
        tabLayout.setupWithViewPager(vp_shopfoodList);


        String shopname = getIntent().getStringExtra("shopname");
        String shoppic = getIntent().getStringExtra("shoppic");
        String url =
                "http://172.24.10.175:8080/foodService/"+shoppic;
        toolbar.setTitle(shopname);

        Picasso.get().load(url).into(imageView);
        imageView.setAlpha((float) 0.5);
    }

    @Override
    void initEvents() {
    }

    @Override
    void initData() {
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.collect, menu);
        this.menuItem = menu.findItem(R.id.item_collect);

        SharedPreferences sharedPreferences = getSharedPreferences("userinfo",MODE_PRIVATE);
        int userid = sharedPreferences.getInt("userid",-1);
        int shopid = getIntent().getIntExtra("shopid",-1);
        shopModel.getCollectstate(userid, shopid, 0, new OnRetrofitListener<Collectstate>() {
            @Override
            public void onSuccess(Collectstate object, int flag) {
                if (object.getCollected().equals("1")){
                    menuItem.setIcon(R.drawable.ic_stars_collect_filled);
                    favFlag = "1";
                }
                else {
                    menuItem.setIcon(R.drawable.ic_stars_collect);
                    favFlag = "0";
                }
            }

            @Override
            public void onFailure(String msg) {
                Toast.makeText(ShopFoodListActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        });
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                break;
            case R.id.item_collect:
                doCollect();
                break;
            default:
                Toast.makeText(this, item.toString()+" is selected.", Toast.LENGTH_SHORT).show();
                return false;
        }
        return true;
    }

    private void doCollect() {
        SharedPreferences sharedPreferences = getSharedPreferences("userinfo",MODE_PRIVATE);
        int userid = sharedPreferences.getInt("userid",-1);
        int shopid = getIntent().getIntExtra("shopid",-1);
        shopModel.doCollect(userid, shopid, new OnRetrofitListener<CollectBean>() {
            @Override
            public void onSuccess(CollectBean object, int flag) {
                if (flag == 5) {
                    if (object.getSuccess().equals("1")) {
                        if (favFlag.equals("1")){
                            Toast.makeText(ShopFoodListActivity.this, "Unstared", Toast.LENGTH_SHORT).show();
                            menuItem.setIcon(R.drawable.ic_stars_collect);
                            favFlag = "0";
                        }
                        else{
                            Toast.makeText(ShopFoodListActivity.this, "Stared", Toast.LENGTH_SHORT).show();
                            menuItem.setIcon(R.drawable.ic_stars_collect_filled);
                            favFlag = "1";
                        }
                    }
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        vp_shopfoodList.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }
}
